//
//  SQMyRechargeViewController.m
//  pai
//
//  Created by pro on 14-11-12.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQMyRechargeViewController.h"
#import "SQRechangeTableViewCell.h"

@interface SQMyRechargeViewController ()

@end

@implementation SQMyRechargeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"充值记录"];
    [self initList];
}



-(void)initList
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height)];
    [self.view addSubview:_scrollView];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = NO;
    _scrollView.scrollEnabled = YES;
    
    //充值记录界面
    _rlv = [SQRechargeListView initmyview];
    _rlv.frame = CGRectMake(0, 0, 320, 200);
    _rlv.delegate = self;
    _rlv.nowPointsLabel.text = [NSString stringWithFormat:@"%ld",(long)[Global instance].userinfo.money];
    [_scrollView addSubview:_rlv];
    
    //列表
    _changeList = [[UITableView alloc]init];
    _changeList.frame =CGRectMake(10, 200, 300, _scrollView.frame.size.height - 200);
    _changeList.delegate = self;
    _changeList.dataSource = self;
    [_changeList setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1]];
    _changeList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:_changeList];
    
    [self getInfo];
}

-(void)getInfo
{
    my_stuff_t stuff;
    stuff.base.msgtype = 1030;
    stuff.base.len = sizeof(stuff);
    stuff.base.error = 0;
    stuff.type = 6001;
    strcpy(stuff.serialnum, "");
    strcpy(stuff.username, [[Global instance].userinfo.username UTF8String]);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&stuff length:sizeof(stuff)];
    
    [super sendData:msgData tag:2 withHUD:YES];
}
-(void)getList
{
    my_stuff_t stuff;
    stuff.base.msgtype = 1030;
    stuff.base.len = sizeof(stuff);
    stuff.base.error = 0;
    stuff.type = 7001;
    strcpy(stuff.serialnum, "");
    strcpy(stuff.username, [[Global instance].userinfo.username UTF8String]);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&stuff length:sizeof(stuff)];
    
    [super sendData:msgData tag:1 withHUD:YES];
}
-(void)gotoRechargeView
{
    [self.tabBarController setSelectedIndex:1];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataMArr.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *orderCellName  = @"SQRechangeTableViewCell";
        SQRechangeTableViewCell *ordercell = (SQRechangeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:orderCellName];
        if (ordercell==nil) {
            ordercell = [[[NSBundle mainBundle] loadNibNamed:@"SQRechangeTableViewCell" owner:self options:nil] lastObject];
        }
        inpour_record_t item;
        [[self.dataMArr objectAtIndex:indexPath.row]getValue:&item];
        [ordercell setItem:item];
        ordercell.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
        return ordercell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击
}

//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSLog(@"get %ld",tag);
    if (tag == 1) {
        if (self.socketLength == 0) {
            ack_my_stuff_t baseInfo;
            [data getBytes:&baseInfo length:sizeof(baseInfo)];
            NSLog(@"%d",baseInfo.base.msgtype);
            NSLog(@"%d",baseInfo.base.len);
            NSLog(@"%d",baseInfo.base.error);
            NSLog(@"%d",baseInfo.count);
            if (baseInfo.base.error != ACS_SUCCESS) {
                [self.HUD showAnimated:YES whileExecutingBlock:^{
                    [self.HUD show:NO];
                } completionBlock:^{
                }];
                return;
            }
            self.templength =  data.length;
            self.socketLength = baseInfo.base.len;
        }
        else{
            self.templength += data.length;
        }
        if (self.socketTempData == nil) {
            self.socketTempData = [NSMutableData dataWithData:data];
        }else{
            [self.socketTempData appendData:data];
        }
        if (self.templength < self.socketLength) {
            [asyncSocket readDataWithTimeout:-1 tag:tag];
            return;
        }else{
            ack_my_stuff_t baseInfo;
            [self.socketTempData getBytes:&baseInfo length:sizeof(baseInfo)];
            inpour_record_t record;
            self.dataMArr = nil;
            self.dataMArr = [NSMutableArray array];
            for (int i =0; i< baseInfo.count; i++) {
                [self.socketTempData getBytes:&record range:NSMakeRange(self.socketTempData.length - (baseInfo.count - i)*sizeof(inpour_record_t)-8,sizeof(inpour_record_t))];
                [self.dataMArr addObject:[NSValue value:&record withObjCType:@encode(struct inpour_record_t)]];
                NSLog(@"%d",record.amt);
                NSLog(@"%d",record.channel);
                NSLog(@"%d",record.inpour_date);
                NSLog(@"%d",record.status);
                
                NSLog(@"%s",record.ex_serialnum);
                
                NSLog(@"%s",record.serialnum);
            }
            self.socketTempData = nil;
            self.socketLength = 0;
            self.templength = 0;
            //加载
            //[self.lowTableView headerEndRefreshing];
            //更新列表
            [self.HUD showAnimated:YES whileExecutingBlock:^{
                [self.HUD show:NO];
            } completionBlock:^{
                [_changeList reloadData];
            }];
        }
    }else if (tag == 2)
    {
        ack_my_stuff_t baseInfo;
        [data getBytes:&baseInfo length:sizeof(ack_my_stuff_t)];
        NSLog(@"%d",baseInfo.base.error);
        NSLog(@"%d",baseInfo.count);
        if (baseInfo.base.error == ACS_SUCCESS) {
            assets_t asset;
            [data getBytes:&asset range:NSMakeRange(data.length - sizeof(asset) - 8,sizeof(asset))];
            _rlv.nowPointsLabel.text = [NSString stringWithFormat:@"%d",asset.ballance];
        }
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            [self.HUD show:NO];
        } completionBlock:^{
            [self getList];
        }];
    }
}

@end
